#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

#=====mysql start
USERNAME="root"
PASSWORD="123456"
DBNAME="mylaw"
TABLENAME="law"
#=====mysql end



function runMysql(){

   docker run  -v "$PWD/mysql-config":"/etc/mysql/mysql.conf.d" \
              -p 3306:3306 \
              --name es-mysql \
              -e MYSQL_ROOT_PASSWORD=$PASSWORD \
              -d mysql:5.7

}

function runEs(){

   docker run -d --name elasticsearch \
          -v "$PWD/es-plugins/ik":"/usr/share/elasticsearch/plugins/ik" \
          -p 9200:9200 -p 9300:9300 \
          -e "discovery.type=single-node"\
          elasticsearch:5.6.8

}

function createEsIndex(){
# 创建索引
curl -XPUT http://localhost:9200/mylaw
# 创建mapping
curl -XPOST http://localhost:9200/mylaw/law/_mapping -H 'Content-Type:application/json' -d'
{
  "properties": {
    "id": {
      "type": "keyword"
    },
    "content": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_max_word"
    },
    "title": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_max_word"
    },
    "remark": {
      "type": "text",
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_max_word"
    },
    "type": {
      "type": "long"
    }
  }
}'

}

function createDB(){
#设置传输大小100M
docker exec es-mysql bash -c "mysql -u${USERNAME} -p${PASSWORD} -e 'set global max_allowed_packet = 100*1024*1024 ;'"
#创建数据库
docker exec es-mysql bash -c "mysql -u${USERNAME} -p${PASSWORD} -e 'create database IF NOT EXISTS ${DBNAME} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'"

#创建表
docker exec es-mysql bash -c "mysql -u${USERNAME} -p${PASSWORD}  -D ${DBNAME} -e 'create table IF NOT EXISTS ${TABLENAME}
(
  id             varchar(255) not null primary key,
  title          varchar(255) not null,
  remark         varchar(255)  null,
  content        longtext         not null,
  type           int(10)      not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;'"


}

runMysql
sleep 30;
createDB
runEs
sleep 30;
createEsIndex

./start.sh